package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.article.dto.ArticleHomeDto;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.article.pojo.ApArticleConfig;
import com.zhentao.model.article.pojo.ApArticleContent;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApArticleContentService;
import com.zhentao.service.ArticleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentService apArticleContentService;

    //设置size常量   当页面传递过来的>50的时候，默认设置的最大数是50
    private final static short MAX_PAGE_SIZE = 50;
    @Override
    public ResponseResult load(Integer loadType, ArticleHomeDto dto) {
        //1.校验参数 -size
        Integer size = dto.getSize();
        if (size == null || size == 0){
            size = 10;
        }
        //如果size<50 size=size 如果size>50 size=50
        size=Math.min(size,MAX_PAGE_SIZE);
        //然后把size赋值给dto
        dto.setSize(size);
        //2.校验参数 -频道
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }
        //3.校验参数 -时间
        if (dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }

        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        //2.查询数据
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, loadType);
        //3.封装结果
        return ResponseResult.okResult(apArticles);
    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
//1.校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.封装参数
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        //3.进行保存  判断是新增还是修改  id
        if (dto.getId() == null) {  //新增
            //保存文章基本信息
            articleMapper.insert(apArticle);
            //保存内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);

            //保存配置表  -自己实现
            ApArticleConfig apArticleConfig=new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfigMapper.insert(apArticleConfig);


        } else {
            //修改文章
            articleMapper.updateById(apArticle);

            //修改内容
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleContent::getArticleId, dto.getId());
            ApArticleContent content = apArticleContentMapper.selectOne(queryWrapper);
            content.setContent(dto.getContent());
            apArticleContentMapper.updateById(content);

            //修改配置表

        }
        //生成url修改数据库
        apArticleContentService.createStaticUrl(apArticle.getId());

        return ResponseResult.okResult(apArticle.getId());
    }

}
